package online.flowerinsnow.minecraft.bukkit.manhunt.util;

import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

public class CommandUtils {
    public static List<String> subCommandFilter(String arg, String... subCommands) {
        List<String> list = new ArrayList<>();
        for (String subCommand : subCommands) {
            if (subCommand.toLowerCase().startsWith(arg.toLowerCase())) {
                list.add(subCommand);
            }
        }
        return list;
    }
    public static List<String> playerSubCommandFilter(CommandSender sender, String arg) {
        List<String> list = new ArrayList<>();
        Player senderP = null;
        if (sender instanceof Player) senderP = (Player) sender;
        Player var0 = senderP;
        Bukkit.getOnlinePlayers().forEach(p -> {
            if (p.getName().toLowerCase().startsWith(arg.toLowerCase())) {
                if (var0 != null && !var0.canSee(p)) return;
                list.add(p.getName());
            }
        });
        return list;
    }
    public static List<String> worldSubCommandFilter(String arg) {
        List<String> list = new ArrayList<>();
        Bukkit.getWorlds().forEach(w -> {
            if (w.getName().toLowerCase().startsWith(arg.toLowerCase())) {
                list.add(w.getName());
            }
        });
        return list;
    }
    public static List<String> enumSubCommandFilter(String arg, Class<? extends Enum<?>> enu) {
        List<String> list = new ArrayList<>();
        try {
            Enum<?>[] enums = (Enum<?>[]) enu.getMethod("values").invoke(null);
            for (final Enum<?> val_4_e : enums) {
                final String name = val_4_e.name();
                if (name.toLowerCase().startsWith(arg.toLowerCase())) {
                    list.add(name);
                }
            }
            return list;
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
            return list;
        }
    }
    public static boolean checkPermission(CommandSender sender, String permission) {
        if (sender == Bukkit.getConsoleSender()) return false;
        if (sender.hasPermission(permission)) return false;
        sender.sendMessage(MessageUtils.parseColor("&7[&c×&7]&c您没有权限 &e" + permission));
        return true;
    }
}
